package com.aiwu.dialog

import android.content.Context
import android.content.DialogInterface
import android.view.View
import androidx.annotation.StringRes
import com.aiwu.bean.ScreenConfig
import com.aiwu.library.abs.ui.AbsQuickSettingBottomActionDialog
import com.aiwu.library.bean.setting.SelectSettingBean
import com.aiwu.library.ui.adapter.SettingMultipleTypeAdapter
import com.aiwu.library.util.ToastUtil
import com.aiwu.utils.PrefsHelper
import com.chad.library.adapter.base.entity.MultiItemEntity
import org.citra.citra_emu.R

/**
 * 快速切换布局
 */
class QuickScreenLayoutDialog(
    context: Context,
    private val fastSettingDialog: FastSettingDialog
) : AbsQuickSettingBottomActionDialog<SettingMultipleTypeAdapter>(context, true, false) {
    private val screenConfigList by lazy {
        SwitchScreenLayoutDialog.getScreenLayoutConfigList()
    }

    override fun onActionBtnClick(view: View, isConfirmBtn: Boolean) {
        dismiss()
    }

    override fun doLogic() {
        super.doLogic()
        settingAdapter.onSelectItemChangeListener =
            SettingMultipleTypeAdapter.OnSelectItemChangeListener { position: Int, newSelectItemIndex: Int ->
                //防止重复
                for (i in 0 until settingAdapter.itemCount) {
                    if (i == position) {
                        continue
                    }
                    val item = settingAdapter.getItem(i) as SelectSettingBean<ScreenConfig>
                    if (item.selectItemIndex == newSelectItemIndex) {
                        item.selectItemIndex = -1
                        settingAdapter.notifyItemChanged(i)
                    }
                }
            }
    }

    override fun getSettingData(): MutableList<MultiItemEntity> {
        return mutableListOf<MultiItemEntity>().apply {
            add(
                SelectSettingBean(
                    getString(R.string.choose_screen_layout_first), null, null,
                    screenConfigList, false, getString(R.string.choose_screen_layout_tip)
                )
            )
            add(
                SelectSettingBean(
                    getString(R.string.choose_screen_layout_second), null, null,
                    screenConfigList, false, getString(R.string.choose_screen_layout_tip)
                )
            )
        }
    }

    override fun getSettingAdapter(): SettingMultipleTypeAdapter = SettingMultipleTypeAdapter()

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        var isEnable = true
        for (i in 0 until settingAdapter.itemCount) {
            val item = settingAdapter.getItem(i) as? SelectSettingBean<*> ?: continue
            if (item.selectItemIndex == -1) {
                isEnable = false
            }
        }
        if (isEnable) {
            val selectItemIndex1 =
                (settingAdapter.getItem(0) as SelectSettingBean<*>).selectItemIndex
            val selectItemIndex2 =
                (settingAdapter.getItem(1) as SelectSettingBean<*>).selectItemIndex
            PrefsHelper.instance.setQuickScreenLayout(
                screenConfigList[selectItemIndex1].id,
                screenConfigList[selectItemIndex2].id
            )
        } else {
            PrefsHelper.instance.removeQuickScreenLayout()
        }
        ToastUtil.toast(
            context.getString(
                R.string.remove_screen_layout_tip,
                if (isEnable) "已开启" else "已关闭"
            )
        )
        fastSettingDialog.refreshQuickScreenLayout(isEnable)
    }

    private fun getString(@StringRes resId: Int): String {
        return context.getString(resId)
    }

    override fun isDialogPauseSimulator(): Boolean = false
}
